
package textmining.gpanalysis;

import edu.smu.tspell.wordnet.Synset;
import edu.smu.tspell.wordnet.SynsetType;
import edu.smu.tspell.wordnet.WordNetDatabase;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.bag.HashBag;

/**
 *
 * @author ibrahimsabek
 */
public class WordNetSynLoader {

    public static final String wordNetDir = "C:/Program Files/WordNet/2.1/dict";

    public WordNetSynLoader(){
        System.setProperty("wordnet.database.dir", wordNetDir);
    }

    public List<String> loadSynonymsList(String wordForm){

        Bag synBag = loadSynonymsBag(wordForm);
        if(synBag != null){
            List<String> synList = new ArrayList<String>();

            //get synonyms with max. frequency
            int maxCount = 0;
            Iterator i= synBag.uniqueSet().iterator();
            while(i.hasNext()){
                String syn = (String)i.next();
                if(maxCount < synBag.getCount(syn)){
                    maxCount = synBag.getCount(syn);
                }
            }

            Iterator j= synBag.uniqueSet().iterator();
            while(j.hasNext()){
                String syn = (String)j.next();
                if(synBag.getCount(syn) == maxCount){
                   synList.add(syn);
                }
            }

            return synList;
        }else{
          return null;
        }
        
    }

    public Bag loadSynonymsBag(String wordForm){

        WordNetDatabase database = WordNetDatabase.getFileInstance();
        Synset[] synsets = database.getSynsets(wordForm);

        boolean foundVerb = false;
        boolean endSearching = false;
        Bag synBag = new HashBag();

        //it has synonyms for this wordform
        if (synsets.length > 0){

            //search for verbs sets and its length is one
            for(int i=0; i<synsets.length; i++){
                if(synsets[i].getType() == SynsetType.VERB){
                    foundVerb = true;
                    String[] currWordForms = synsets[i].getWordForms();

                    //found the exact verb
                    if(currWordForms.length == 1){
                        endSearching = true;
                        synBag.add(currWordForms[0]);
                        break;
                    }
                }
            }

            //search for verbs sets and has any length
            if(!endSearching){
                for(int i=0; i<synsets.length; i++){
                    if(synsets[i].getType() == SynsetType.VERB){
                        foundVerb = true;
                        endSearching = true;

                        //add all verbs to get the most common one from them
                        String[] currWordForms = synsets[i].getWordForms();
                        for(String currWordForm : currWordForms){
                            synBag.add(currWordForm);
                        }
                            
                    }
                }

            }
            
            if(!foundVerb && !endSearching){

                //search for nouns and adjectives with length of one
                for(int i=0; i<synsets.length; i++){
                    String[] currWordForms = synsets[i].getWordForms();

                    //found the exact synonym
                    if(synsets[i].getType() == SynsetType.NOUN && currWordForms.length == 1){
                        endSearching = true;
                        synBag.add(currWordForms[0]);
                        break;
                    }
                }

                if(!endSearching){
                    for(int i=0; i<synsets.length; i++){
                        String[] currWordForms = synsets[i].getWordForms();

                        //found the exact synonym
                        if(synsets[i].getType() == SynsetType.ADJECTIVE && currWordForms.length == 1){
                            endSearching = true;
                            synBag.add(currWordForms[0]);
                            break;
                        }
                    }
                }

                //search for synonym sets and has any length
                if(!endSearching){
                    for(int i=0; i<synsets.length; i++){
                        endSearching = true;

                        //add all synonyms to get the most common one from them
                        String[] currWordForms = synsets[i].getWordForms();
                        for(String currWordForm : currWordForms){
                            if(currWordForm.matches(".*"+wordForm+".*") || wordForm.matches(".*"+currWordForm+".*")){
                                synBag.add(currWordForm);
                            }
                        }

                    }

                }

            }
             return synBag;
        }else{
             return null;
        }

    }
    
}
